<?php
namespace Mobile\Controller;
use Think\Controller;
use Common\User;

class UserController extends PublicController {
	public $user_id = 0;
	
	public function _initialize() {      
        parent::_initialize();
		
		$this->user = new User();
		
		$nologin = array("login",'actlogin');
		$this->user_id = $_SESSION['user_id'];
		
		
		if(!in_array(ACTION_NAME,$nologin)){
			if(empty($_SESSION['user_id'])){
				$this->redirect("User/login");
			}
		}else{
			if($_SESSION['user_id'] > 0){
				$this->redirect("User/index");
			}
		}
	}
	
    public function index(){
		echo 123;exit;
		$this->display();
        //$this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
    }
	
	/*
	*登录页面
	*/
	public function login(){
		$this->view();
	}
	
	/*
	*处理登陆数据
	*/
	public function actlogin(){
		if (IS_POST) {
			$username = I('post.username');
            $password = I('post.password');
			
			$username = trim($username);
			$password = trim($password); 
			
			if(empty($username)){
				$this->error('用户名不能为空',U('User/login'));
			}
			
			if(empty($password)){
				$this->error('密码不能为空',U('User/login'));
			}
			
			
			//进行登录操作
			$have_user = $this->user->check_user_name($username);
			if($have_user <= 0){
				$this->error('用户不存在',U('User/register'));
			}
			
			$result = $this->user->login($username,$password,I('post.remember'));
			
			if($result['status'] == 0){
				$this->error($result['msg'],U('User/login'));
			}else if($result['status'] == 1){
				$this->success($result['msg'],U('User/index'));
			}
		}
	}

	
	/*
	*退出登录页面
	*/
	public function logout(){
		$this->user->logout();
		
		$this->redirect("User/login");
	}

}